package com.example.androidproject.sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "sqlite.db";
    private static final int DATABASE_VERSION = 1; // 保持版本为 1

    private String tableName;
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_MESSAGE = "message";
    public static final String COLUMN_IDENTITY = "identity";


    public DatabaseHelper(Context context, String tableName) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.tableName = tableName;
    }

    private String getTableCreateStatement() {
        return "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_MESSAGE + " TEXT, " +
                COLUMN_IDENTITY + " INTEGER" +
                ");";
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
//        Log.d("???", "Table created: " + tableName);
    }

    public void createTable(SQLiteDatabase db){
        db.execSQL(getTableCreateStatement());
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 由于保持版本为 1，这里不会执行任何操作
        // 如果未来需要支持升级，可以考虑在新版本中添加相关逻辑
        db.execSQL("DROP TABLE IF EXISTS " + tableName);
        onCreate(db);
    }
}
